"""
@name : 词频统计
@author : wangaokang
@projectname: pythonProject6
"""

import re

with open("walden.txt","r",encoding="utf-8")as fp:
    file = fp.read()
    file_lower = file.lower()

    new_file = re.sub(r'[\d",\n/?+:.;!$-]', ' ', file_lower)
    new_dict = new_file.split()

    #定义一个字典

    Dict = dict()
    for i in new_dict:
        if i in Dict:
            Dict[i] += 1

        else:
            Dict[i] = 1


    result = sorted(Dict.items(), key=lambda item: item[1], reverse=True)

    for i,j in result:
        print(f"{i}总共在文章中出现了{j}次")
